package com.lzy.managersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.managersystem.pojo.Dict;
import com.lzy.managersystem.pojo.Menu;
import com.lzy.managersystem.service.DictService;
import com.lzy.managersystem.service.MenuService;
import com.lzy.managersystem.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private DictService dictService;

    @GetMapping("/")
    public Result selectAll(@RequestParam(defaultValue = "") String name) {
        List<Menu> list = menuService.getAllMenu(name);
        return Result.success(list);
    }

    @PostMapping("/inster")
    public Result insertUser(@RequestBody Menu menu) {
        System.out.println(menu);
        int flag = menuService.insterUser(menu);
        return Result.success(flag);
    }

    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        int flag = menuService.deleteUser(id);
        return Result.success(flag);
    }

    @PostMapping("/del/batch")
    public Result deleteBatchUser(@RequestBody List<Integer> ids) {
        int flag = menuService.deleteBatchUser(ids);
        return Result.success(flag);
    }

    @PutMapping("/")
    public Result UpdataUser(@RequestBody Menu menu) {
        int flag = menuService.updateUser(menu);
        return Result.success(flag);
    }

    @GetMapping("/dict")
    public Result dict() {
        List<Dict> dicts = dictService.selectall();
        return Result.success(dicts);
    }


    //查询所有用户（分页）
    @GetMapping("/page")
    public Result selectUserPage(@RequestParam Integer pageSize,
                                 @RequestParam Integer pageNum,
                                 @RequestParam(defaultValue = "") String name
    ) {
        IPage<Menu> MenuIPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Menu> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name", name);
//        userQueryWrapper.orderByDesc("id");
        return Result.success(menuService.page(MenuIPage, userQueryWrapper));

    }
}
